#include "testwindow.h"
#include "ui_testwindow.h"
#include"../utils/database.h"
#include<QSqlQuery>
#include<QDebug>
#include<QSqlError>
#include<QBuffer>
#include<QPixmap>

TestWindow::TestWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestWindow)
{
    ui->setupUi(this);

    test();

//    imageUpdate();

//    QSqlQuery result=QSqlQuery(*DataBase::getInstance());
//    QPixmap image=QPixmap("E:\\2022_first\\PM_Discuss\\procedure\\test\\canteen-management-system\\DataHandler\\data/default.png");
//    QByteArray bytes;
//    QBuffer buffer(&bytes);
//    buffer.open(QIODevice::WriteOnly);
//    image.save(&buffer,"png");
//    QVariant imageData(bytes);

//    QString sql="insert into picture(id,content,size)"
//                "value(0,:co,:si);";

//    result.prepare(sql);
//    result.bindValue(":co",imageData);
//    result.bindValue(":si",bytes.size());
//    if(!result.exec())
//    {
//        qDebug()<<result.lastError().text();
//    }
////    获取上一个插入id
//    if(!result.exec("SELECT  @@id;"))
//        qDebug()<<result.lastError().text();
//    result.next();
//    int image_id=result.value(0).toInt();
//    image_id=100;
//    sql="select content from picture where id = %1;";
//    sql=sql.arg(image_id);
//    if(!result.exec(sql))
//    {
//        qDebug()<<result.lastError().text();
//    }
//    result.next();
//    image.loadFromData(result.value(0).toByteArray());
//    ui->label->setPixmap(image);
//    qDebug()<<image;
}

TestWindow::~TestWindow()
{
    delete ui;
}

void TestWindow::imageUpdate()
{
    QString path="E:/2022_first/PM_Discuss/procedure/test/canteen-management-system/DataHandler/data/%1.png";
    QSqlQuery result=QSqlQuery(*DataBase::getInstance());
    for(int i=1;i<10;i++)
    {
        QString tp=path.arg(i+100);
        QPixmap image=QPixmap(tp);
        QByteArray bytes;
        QBuffer buffer(&bytes);
        buffer.open(QIODevice::WriteOnly);
        image.save(&buffer,"png");
        QVariant imageData(bytes);

        QString sql="insert into picture(id,content,size) "
                     "value(:id,:co,:si);";

        result.prepare(sql);
        result.bindValue(":co",imageData);
        result.bindValue(":si",bytes.size());
        result.bindValue(":id",i+100);
        if(!result.exec())
        {
            qDebug()<<result.lastError().text();
        }
    }

    for(int i=10;i<=65;i++)
    {
        QPixmap image=QPixmap(path.arg(i));
        QByteArray bytes;
        QBuffer buffer(&bytes);
        buffer.open(QIODevice::WriteOnly);
        image.save(&buffer,"png");
        QVariant imageData(bytes);

        QString sql="insert into picture(id,content,size)"
                    "value(:id,:co,:si);";

        result.prepare(sql);
        result.bindValue(":co",imageData);
        result.bindValue(":si",bytes.size());
        result.bindValue(":id",i);
        if(!result.exec())
        {
            qDebug()<<result.lastError().text();
        }
    }
}


void TestWindow::test()
{

}

